<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
set_time_limit(0);
$intSuppleID = (int)$_data["SuppleID"];
$intBT = (int)$_data["Bet_Type"];
$intMemID = (int)$_data["MemID"];
$intGID = (int)$_data["GambleID"];
$flOdds = (float)$_data["Odds"];
$flOdds2 = (float)$_data["Odds2"];
$flStakeMoney = (float)$_data["StakeMoney"];
$isSpecial = trim($_data["isSpecial"]);
$strSNo = trim($_data["SerialNo"]);
$intSpecialNumType = (int)$_data["SpecialNumType"];
$strChoiceNumber = trim($_data["ChoiceNumber"]);

if(($intSpecialNumType == 2 || $intSpecialNumType == 4) && ($intBT == 1 || $intBT == 28))$intBT = 28;

//檢驗傳入資料
if($intBT < 1 || $intGID < 1 || $intLType != 1 || $intLottoID < 1 || $flOdds <= 0 || $flStakeMoney <= 0 || $intMemID <= 0 || $strSNo == "" || (($intBT == 8 || $intBT == 10) && $flOdds2 <= 0)){
	$GLOBALS["errormessage"] = "Data Error!!";
	$GLOBALS["errorhidden"] = "\nBetType:".$intBT."\nGambleID:".$intGID."\nLottoID:".$intLottoID."\nflOdds:".$flOdds."\nStakeMoney:".$flStakeMoney."\nMemID:".$intMemID."\nSerialNo:".$strSNo."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

//取得補貨帳號資料
$strSQL = "select * from `bet_Member` where MemberGroup = 21 and isLock = 0 and ID = ".(string)$intMemID." limit 0,1";
if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errorhidden"] = "Find User Data Error!";
	$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}

if(@PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errorhidden"] = "Can not Find User Data!!";
	$GLOBALS["errorhidden"] = "SQL: ".$strSQL.", \nError: ".PMA_mysql_error($$_Conn).", \nLine: ".__LINE__.", \nPage: ".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
$objMem = PMA_mysql_fetch_object($RS);

//找出會員退水
$strSQL = "select * from `bet_Commission` where MemID = ".$objMem -> ID." and Gamble_Type = ".$objMem -> GambleType." and Bet_Type = '".$intBT."' and LottoID = '".$intLottoID."' limit 0, 1";
if(!$RS = PMA_mysql_query($strSQL, $$_Conn)){
	$GLOBALS["errormessage"] = "Find Commission Data Error!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
if(PMA_mysql_num_rows($RS) == 0){
	$GLOBALS["errormessage"] = "Can not Find Commission Data!!";
	$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nSQL:".$strSQL."\nLine:".__LINE__."\nPage:".__FILE__;
	ErrMsgOutputHTML("Error", "", 1);
}
$Rows = @mysql_fetch_object($RS);
$intComm = $Rows -> Commission;


//組合陣列 for bet_BetListContent 用
$arrWCont["GameDate_x"] = $_data["GameDate"];
$arrCheckKey = split(" ",microtime());
$arrWCont["checkkey_x"] = str_replace("0.", $arrCheckKey[1]."-", $arrCheckKey[0]);
$arrWCont["GambleType"] = $objMem -> GambleType;
$arrWCont["MemAccount_x"] = $objMem -> Account;
$arrWCont["isBlack"] = $objMem -> isBlack;
$isJoinWager = false;
$isParlays = false;
$ChoiceGroup = 1;
$arrJoinMinOdds = null;
switch($intBT){
	case 8:
	case 10:
		$arrWList["OddsID2"] = -999;
		$arrWList["Odds2"] = $flOdds2;

	case 7:
	case 9:
	case 11:
		$GroupCounter = ($intBT == 7 || $intBT == 8)?3:2;
		$arrWCont["JoinNumber_x"] = str_replace(" ", "", $_data["Number"]);
		$JoinArray = explode(",", $arrWCont["JoinNumber_x"]);
		foreach($JoinArray as $key => $val){
			if($JoinArray[$key] == "")continue;
			$arrTmp[] = sprintf("%02s", $val);
		}
		$JoinArray = $arrTmp;
		array_unshift($JoinArray, "NOUSE");
		unset ($JoinArray[0]);
		$arrWCont["JoinNumber_x"] = join(",", $JoinArray);
		//取得連碼陣列
		$JoinArray	= combination($JoinArray, $GroupCounter);
		if(!is_array($JoinArray)){
			$GLOBALS["errormessage"] = "Number Error!!";
			$GLOBALS["errorhidden"] = "\nNumber:".$_data["Number"]."\nLine:".__LINE__."\nPage:".__FILE__;
			if(preg_match("/usbet88/", $_SERVER["HTTP_HOST"])){
				$email = "pinus@uuss.net";
				$subject = "補貨錯誤";
				$content = $GLOBALS["errorhidden"].__LINE__;
				mail($email, $subject, $content, "From: usbet88.us<apache@secure.usbet88.us>");
			}
			ErrMsgOutputHTML("Error", "", 1);
		}
		//取得連碼組數
		$ChoiceGroup	= count($JoinArray);

		$arrWCont["OneStake"]	= $flStakeMoney*-1;
		$arrWList["Number"] = "0";
		$isJoinWager = true;
		//取得下注內容
		$strWagerConditions = getWagerConditions($intLType, $intBT, "0", $flOdds, $strSNo, $flOdds2, split(",", $arrWCont["JoinNumber_x"]), 1);
		$_data["Number"] = 0;
		break;

	case 30:
	case 31:
	case 32:
	case 33:
	case 34:
	case 35:
	case 36:
	case 37:
	case 38:
	case 39:
	case 40:
	case 41:
	case 42:
	case 43:
		$intMinN = 0;
		$intMaxN = 9;
		if($intBT == 31){
			$intMinN = 1;
			$intMaxN = 49;
		}elseif($intBT >= 32 && $intBT <= 37 || $intBT == 30){
			$intMinN = 1;
			$intMaxN = 12;
		}
		$arrStNumber = array(30 => 6, 31 => 5, 32 => 2, 33 => 3, 34 => 4, 35 => 2, 36 => 3, 37 => 4, 38 => 2, 39 => 3, 40 => 4, 41 => 2, 42 => 3, 43 => 4);
		$arrCNumber = explode(",", $strChoiceNumber);
		$arrCN = array();
		foreach($arrCNumber as $strN){
			$intN = (int)$strN;
			if($intN < $intMinN || $intN > $intMaxN)continue;
			$arrCN[$intN] = $intN;
			$arrNumStake[$intN] += $flStakeMoney / $arrStNumber[$intBT];
			$arrNumOdds[$intN] = $flOdds;
		}
		if(sizeof($arrCN) != $arrStNumber[$intBT]){
			$GLOBALS["errormessage"] = "Number Error!!";
			$GLOBALS["errorhidden"] = "\nNumber:".$strChoiceNumber."\nLine:".__LINE__."\nPage:".__FILE__;
			ErrMsgOutputHTML("Error", "", 1);
		}
		sort($arrCN);
		//取得下注內容
		$strWagerConditions = getWagerConditions($intLType, $intBT, 0, null, $strSNo, null, $arrCN, 1, $arrNumOdds);
		$arrWList["Number"] = 0;
		$arrWCont["JoinNumber_x"] = $strChoiceNumber = implode(",", $arrCN);
		$arrChoiceNumber = $arrCN;
		$isJoinWager = true;
		$strJoinArray = implode(".", $arrCN);
		$arrJoinMinOdds = array($strJoinArray => $flTOdds);
		$JoinArray = $arrCN;
		$isParlays = true;
		break;
	
	default:
		//取得下注內容
		$strWagerConditions = getWagerConditions($intLType, $intBT, $_data["Number"], $flOdds, $strSNo);
		$arrWList["Number"] = $_data["Number"];
		break;
}
$arrWCont["Content_x"] = $strWagerConditions;

$flMemPayOff = ($flStakeMoney*($flOdds - 1));
$flMemERate = (empty($_ECRate[$objMem -> ERID]["Rate"]))?1:$_ECRate[$objMem -> ERID]["Rate"];
//組合陣列 for bet_BetList 用
$arrWList["LottoID"] = $intLottoID;
$arrWList["Bet_Type"] = $intBT;
$arrWList["GambleID"] = $intGID;
$arrWList["OddsID"] = -999;
$arrWList["Odds"] = $flOdds;
$arrWList["MemID"] = $objMem -> ID;
$arrWList["MemExchangeRate"] = $flMemERate;
$arrWList["MemCurrency"] = $objMem -> ERID;
$arrWList["MemStake"] = $flStakeMoney*$ChoiceGroup*-1;
$arrWList["MemPayOff"] = $flMemPayOff*-1;
$arrWList["MemCommission"] = 1 - ($intComm / 100);
$arrWList["SubID"] = $objMem -> ID;
$arrWList["SubCommission"] = 1 - ($intComm / 100);
$arrWList["SubRate"] = 1;
$arrWList["MainID"] = $objMem -> ID;
$arrWList["MainCommission"] = 1 - ($intComm / 100);
$arrWList["MainRate"] = 1;
$arrWList["StockID"] = $objMem -> ID;
$arrWList["StockCommission"] = 1 - ($intComm / 100);
$arrWList["StockRate"] = 1;
$arrWList["DirectorID"] = $objMem -> ID;
$arrWList["DirectorCommission"] = 1 - ($intComm / 100);
$arrWList["DirectorRate"] = 1;
$arrWList["PayupDate_x"] = $_data["GameDate"];
$arrWList["WagerType"] = 1;

//外調下注
if($_data["isGetSuppleOdds"] == "Y")$arrWList["isPayOff"] = -2;

PMA_mysql_query("BEGIN", $_ConnMaster);
//寫入注單
$arrBetData = InsertWagerToDB($arrWCont, $arrWList);
if($arrBetData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMaster);
	$GLOBALS["errormessage"] = $arrBetData["ErrorMessage"];
	$GLOBALS["errorhidden"] = $arrBetData["ErrorHiddenMessage"];
	AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
	ErrMsgOutputHTML("Error", "", 1);
}

//寫入下注號碼 ParlaysNumberEven ... 是 Even 喔
if($isJoinWager){
	$arrBetParlaysEvenData = InsertParlaysNumberEvenToDB($arrWList["MemID"], $arrWList["SubID"], $arrWList["MainID"], $arrWList["StockID"], $arrWList["DirectorID"], $arrWList["GambleID"], $arrWList["Bet_Type"], $arrBetData["BetID"], $JoinArray, $flStakeMoney*-1, $arrWList["Odds"], $arrWList["Odds2"], $arrJoinMinOdds);
	if($arrBetParlaysEvenData["isError"]){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$GLOBALS["errormessage"] = $arrBetParlaysEvenData["ErrorMessage"];
		$GLOBALS["errorhidden"] = $arrBetParlaysEvenData["ErrorHiddenMessage"];
		print $GLOBALS["errorhidden"];
		AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
		ErrMsgOutputHTML("Error", "", 1);
	}
}

//更改 TodayGame 狀態
if($_data["isGetSuppleOdds"] != "Y"){
	$flStakeMoney = $flStakeMoney * $flMemERate*$ChoiceGroup;
	$flMemPayOff *= $flMemERate;
	$flComm = $flStakeMoney*$arrWList["DirectorCommission"];
	if($isParlays){
		$intNumNo = sizeof($arrChoiceNumber);
		$flStakeMoney = $flStakeMoney / $intNumNo;
		$flMemPayOff = $flMemPayOff / $intNumNo;

		foreach($arrChoiceNumber as $k => $intNumTmp){
			$strOhterCase = " tolSuppleWager = 1 + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".$flStakeMoney.", tolSupplePayOff = tolSupplePayOff + ".$flMemPayOff.", tolSuppleCommission = tolSuppleCommission + ".$flComm;

			if(!UpdateTodayGameOhterCase($intGID, $intLottoID, $intBT, $intNumTmp, $strOhterCase)){
				PMA_mysql_query("ROLLBACK", $_ConnMaster);
				AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
				ErrMsgOutputHTML("Error", "", 1);
			}

			$arrBT[$k] = $intBT;
			$arrOddsID[$k] = -999;
			$arrOdds[$k] = $flOdds;
		}
		$arrBetParlaysData = InsertParlaysThroughToDB($arrBetData["BetID"], $intGID, $arrBT, $arrChoiceNumber, $arrOddsID, $arrOdds);
		if($arrBetParlaysData["isError"]){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			$GLOBALS["errormessage"] = $arrBetParlaysData["ErrorMessage"];
			$GLOBALS["errorhidden"] = $arrBetParlaysData["ErrorMessage"].$arrBetParlaysData["ErrorHiddenMessage"];
			AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
			ErrMsgOutputHTML("Error", "", 1);
		}

	}else{
		$strOhterCase = " tolSuppleWager = 1 + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".$flStakeMoney.", tolSupplePayOff = tolSupplePayOff + ".$flMemPayOff.", tolSuppleCommission = tolSuppleCommission + ".$flComm;
		if(!UpdateTodayGameOhterCase($intGID, $intLottoID, $intBT, $_data["Number"], $strOhterCase)){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
			ErrMsgOutputHTML("Error", "", 1);
		}
	}
}else{
	//設定注單排隊
	if(!setWagerOrder($arrBetData["BetID"], 0)){
		PMA_mysql_query("ROLLBACK", $_ConnMaster);
		$GLOBALS["errormessage"] = $arrBetData["ErrorMessage"];
		$GLOBALS["errorhidden"] = $arrBetData["ErrorHiddenMessage"];
		AddData_Log($_SESSION["Admin_Info"] -> ID, "補貨下注失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
		ErrMsgOutputHTML("Error", "", 1);
	}
}
if(!PMA_mysql_query("COMMIT", $_ConnMaster)){
	if(!rmWagerOrder($arrBetData["BetID"])){
		AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
		ErrMsgOutputHTML("Error", "", 1);
	}
}

//外調下注
if($_data["isGetSuppleOdds"] == "Y"){
	$strNumber = ($arrWList["Bet_Type"] >= 7 && $arrWList["Bet_Type"] <= 11)?$arrWCont["JoinNumber_x"]:$arrWList["Number"];
	$arrResult = SuppleWager($arrWList["LottoID"], $intLType, $arrWList["Bet_Type"], $strNumber, $flStakeMoney, $_data["OddsID"], $arrWList["Odds"], $_data["OddsID2"], $arrWList["Odds2"], $strChoiceNumber, $arrCheckKey[0],  $arrCheckKey[1]+1, $arrBetData["BetID"]);
	if($arrResult["isWager"] == false){
		if(!rmWagerOrder($arrBetData["BetID"])){
			AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
		}
		ErrMsgOutputHTML("Error", "", 1);
	}else{
		PMA_mysql_query("BEGIN", $_ConnMaster);
		$arrSQL[0] = "update `bet_BetList` set isPayOff = 0 where ID in (".$arrBetData["BetID"].")";
		$arrSQL[1] = "update `bet_BetListTmp` set isPayOff = 0 where ID in (".$arrBetData["BetID"].")";
		foreach($arrSQL as $strSQL){
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				PMA_mysql_query("ROLLBACK", $_ConnMaster);
				$GLOBALS["errormessage"] = "System Error!!";
				$GLOBALS["errorhidden"] = "\n Error: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLine: ".__LINE__.",\nPage: ".__FILE__;
				AddData_Log($_SESSION["Admin_Info"] -> ID,"外調補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
				if(!rmWagerOrder($arrBetData["BetID"])){
					AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
				}
				ErrMsgOutputHTML("Error", "", 1);
			}
		}
		$flStakeMoney = $flStakeMoney * $flMemERate*$ChoiceGroup;
		$flMemPayOff *= $flMemERate;
		$flComm = $flStakeMoney*$arrWList["DirectorCommission"];
		$strOhterCase = " tolSuppleWager = 1 + tolSuppleWager, tolSuppleStake = tolSuppleStake + ".$flStakeMoney.", tolSupplePayOff = tolSupplePayOff + ".$flMemPayOff.", tolSuppleCommission = tolSuppleCommission + ".$flComm;
		if(!UpdateTodayGameOhterCase($intGID, $intLottoID, $intBT, $_data["Number"], $strOhterCase)){
			PMA_mysql_query("ROLLBACK", $_ConnMaster);
			AddData_Log($_SESSION["Admin_Info"] -> ID,"外調補貨下注失敗", $arrBetData["LogData"]."\n".$GLOBALS["errorhidden"]);
			if(!rmWagerOrder($arrBetData["BetID"])){
				AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
			}
			ErrMsgOutputHTML("Error", "", 1);
		}
		PMA_mysql_query("COMMIT", $_ConnMaster);
	}
}
if(!rmWagerOrder($arrBetData["BetID"])){
	AddData_Log($_SESSION["Admin_Info"] -> ID, "移除下注排序失敗", join("\n", $arrLogData)."\n".$GLOBALS["errorhidden"]);
}

AddData_Log($_SESSION["Admin_Info"] -> ID,"補貨下注成功 ", $arrBetData["LogData"]);
?>